package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;

/**
 * Created by wzh on 2016/5/6.
 */
public class OpenStoreActivity extends BaseActivity {
    private String TYPE_CER = "";
    private String TYPE_STROE = "";
    private int TYPE = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_store);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText(R.string.apply_store);
        if (savedInstanceState != null) {

            TYPE_CER = getIntent().getStringExtra(Constant.KEY_TYPE_CER_STATUS);
            TYPE_STROE = getIntent().getStringExtra(Constant.KEY_TYPE_STORE_STATUS);
            getIntent().putExtra(Constant.KEY_TYPE_CER_STATUS, TYPE_CER);
            getIntent().putExtra(Constant.KEY_TYPE_STORE_STATUS, TYPE_STROE);
        }
        TYPE_CER = getIntent().getStringExtra(Constant.KEY_TYPE_CER_STATUS);
        TYPE_STROE = getIntent().getStringExtra(Constant.KEY_TYPE_STORE_STATUS);

        if (Constant.CER_STATUS_HAS_CERTIFIED.equals(TYPE_CER) && Constant.STORE_STATUS_UNAUDITING.equals(TYPE_STROE)) {
            //认证审核通过，未开店
            initViewStub(R.layout.layout_open_1);
        } else if (Constant.CER_STATUS_CERTIFING.equals(TYPE_CER)
                || Constant.STORE_STATUS_AUDITING.equals(TYPE_STROE)) {
            //审核中
            initViewStub(R.layout.layout_open_2);
        } else {
            //审核失败
            initViewStub(R.layout.layout_open_3);
        }

        if (Constant.STORE_STATUS_UNAUDITING.equals(TYPE_STROE)) {
            //未开店，进入开店编辑界面，无需请求网络
            TYPE = Constant.TYPE_STORE_STATUS_FIRST_OPEN;
        } else {
            TYPE = Constant.TYPE_STORE_STATUS_EDIT;
        }


    }

    private void initViewStub(int layout) {
        ViewStub detail = (ViewStub) findViewById(R.id.view_detail);
        detail.setLayoutResource(layout);
        detail.setVisibility(View.VISIBLE);

        if (layout == R.layout.layout_open_1) {
            TextView openStore = (TextView) findViewById(R.id.open_store);
            openStore.setOnClickListener(this);
            TextView knowStore = (TextView) findViewById(R.id.know_store);
            knowStore.setOnClickListener(this);
        } else if (layout == R.layout.layout_open_2) {
            TextView knowStore = (TextView) findViewById(R.id.know_store);
            if (Constant.CER_STATUS_CERTIFING.equals(TYPE_CER)
                    && Constant.STORE_STATUS_UNAUDITING.equals(TYPE_STROE)) {
                knowStore.setText("优先一步，去填店铺信息");
            }else if (Constant.CER_STATUS_CERTIFING.equals(TYPE_CER) && Constant.STORE_STATUS_PASSED.equals(TYPE_STROE)){
                knowStore.setText("去修改填店铺信息");
            }
            knowStore.setOnClickListener(this);
            TextView howTime = (TextView) findViewById(R.id.how_time);
            howTime.setOnClickListener(this);
        } else {
            TextView tvReject = (TextView) findViewById(R.id.tv_reject);

            if (Constant.CER_STATUS_REJECTED.equals(TYPE_CER)&&Constant.STORE_STATUS_REGECTED.equals(TYPE_STROE)){
                tvReject.setText("实名信息、开店信息未通过");
            }else if (Constant.CER_STATUS_REJECTED.equals(TYPE_CER)){
                tvReject.setText("实名信息未通过");
            }else {
                tvReject.setText("开店信息未通过");
            }
            TextView openStore = (TextView) findViewById(R.id.re_open_store);
            openStore.setOnClickListener(this);
            TextView whyRefuse = (TextView) findViewById(R.id.why_refuse);
            whyRefuse.setOnClickListener(this);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putInt(Constant.KEY_STORE_TYPE_STATUS, TYPE);
        outState.putString(Constant.KEY_TYPE_CER_STATUS, TYPE_CER);
        outState.putString(Constant.KEY_TYPE_STORE_STATUS, TYPE_STROE);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.open_store:
                intoStorePage();
                break;
            case R.id.why_refuse:
                break;
            case R.id.how_time:
                break;
            case R.id.know_store:
                checking();
                break;
            case R.id.re_open_store:
                reCommit();
                break;
        }
    }

    private void reCommit() {
        if (Constant.CER_STATUS_REJECTED.equals(TYPE_CER)){
            Intent intent = new Intent(this, SelectNameEnsureActivity.class);
            intent.putExtra(Constant.KEY_TYPE_STORE_STATUS, TYPE_STROE);
            intent.putExtra(Constant.KEY_TYPE_CER_STATUS, TYPE_CER);
            startActivity(intent);
        }else {
            Intent intent = new Intent(this, StoreActivity.class);
            intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, TYPE);
            startActivity(intent);
        }
    }

    private void checking() {
        if (Constant.CER_STATUS_CERTIFING.equals(TYPE_CER)
                && Constant.STORE_STATUS_UNAUDITING.equals(TYPE_STROE)) {
            Intent intent = new Intent(this, StoreActivity.class);
            intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, TYPE);
            startActivity(intent);
        }else if (Constant.CER_STATUS_CERTIFING.equals(TYPE_CER) && Constant.STORE_STATUS_PASSED.equals(TYPE_STROE)){
            Intent intent = new Intent(this, StoreActivity.class);
            intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, TYPE);
            startActivity(intent);
        }else {

        }


    }

    public void intoStorePage() {
        Intent intent = new Intent(this, StoreActivity.class);
        TYPE = Constant.TYPE_STORE_STATUS_FIRST_OPEN;
        intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, TYPE);
        startActivity(intent);
    }

}
